home *** CD-ROM | disk | FTP | other *** search
- Memory Allocation Procedures
-
-
- The procedures included in this archive provide functionally equivalent
- memory allocation procedures for the main memory procedures used in C.
- These procedures were written primarily because of a need to minimize
- memory usage for a large program under development. The MSC 5.1
- library procedures were not satisfactory for this application because
- they did not release memory back to the system when it was 'free'ed.
- Additionally, they tended to allocate too much memory under certain
- circumstances which lead to a reduction in space available for other
- programs and data which were currently active at the time.
-
- These procedures should only be used with large data model programs
- (i.e., compact, large, and huge memory models). No provision has been
- made for small or medium model programs. A brief description of each
- file in this archive is given below:
-
- alloc.h - Include file which is used to define the memory
- structures used in processing.
-
- calloc.c - Functional equivalent of calloc.
-
- dispsize.c - Procedure which displays the various sizes of the
- program at the time it is called. Sizes displayed
- are code size, default data size used, default data
- size allocated, memory allocated by user, memory
- allocated by these procedures to hold user
- allocation, and total program size. This is handy
- in determining when and where memory gets allocated
- and released.
-
- dumpheap.c - Procedure which will traverse through the allocated
- memory blocks and write to a file the allocation
- sizes indicating whether they are in use or are
- free. This is a handy procedure for determining
- if information is being freed or not.
-
- free.c - Functional equivalent to free. Included in this
- file is an additional procedure _ffree which calls
- free.
-
- malloc.c - Functional equivalent of malloc. This file also
- contains the memory structures which are used to
- keep track of the blocks of memory allocated. In
- addition, the procedure _fmalloc is defined which
- just makes a call to malloc.
-
- memutil.c - This file contains a set of procedures which are
- used by the main memory allocation procedures to
- maintain the memory structure used to keep track of
- the user allocations and frees.
-
-
- progsize.c - Procedure which when called will return the various
- sizes indicated above in dispsize.c. This is the
- procedure which dispsize.c calls to get it's
- information.
-
- realloc.c - Functional equivalent of realloc.
-
- reduceal.c - This procedure may be called to reduce the memory
- allocated to a program. It attempts to release back
- to DOS the memory not currently being used.
-
- reducedd.c - This procedure may be called to reduce the default
- data segment to the minimum size required. Normally
- this should be called before any calls to memory
- allocation procedures. Included in this file are
- dummy procedures for _nmalloc and _nfree. These
- latter procedures were included because some
- of the MSC library uses the near heap for memory
- allocation (even though you may be using large
- data model programs). Since this procedure does
- away with any space which might be used for near
- heap allocation, this forces the procedures which
- call them to use the 'far' heap.
-
- test.c - This is a program which may be used to compare the
- functionality of these procedures with those found
- in the MSC 5.1 library.
-
- linker.bat - Batch procedure for linking the test program.
-
- load - Link directives for the linking the test program.
-
- For the most part, all of these procedures are commented and indicate
- fairly well what they are doing. As mentioned above, the main purpose
- in writing these procedures was to try to minimize the amount of memory
- used. Consequently, they take extra care in trying to place allocated
- memory at the lowest possible memory location so that higher memory, if
- freed, can be released back to the operating system. This is
- especially true of 'realloc' which tries very hard to place a
- reallocated block at the lowest possible memory location. Don't assume
- that reallocating a block at the same or smaller memory size will
- remain at the same starting address in memory (which isn't true with
- the realloc in this archive). This 'realloc' approach can have some
- advantages if after allocating and freeing a large number of memory
- blocks, you reallocate the blocks you want to save so they percolate
- down into low memory.
-
- Documentation for these procedures is essentially as indicated in the
- MSC 5.1 manuals. There is a global variable called MBSize which may be
- set in the same manner as _amblksiz with the same functionality. See
- the code for malloc.c for further information. Also be aware that if a
- zero length block is requested from malloc, it will return a NULL
- pointer as indicated in the MSC 5.1 manuals. This seems to be
- different than what the MSC procedures do because, at least in my
- version of MSC 5.1, MSC's malloc returns a non-NULL pointer for a zero
- length block request (even though the documentation says otherwise).
-
- All of these procedures were tested fairly thoroughly although I won't
- guarantee there are no bugs. The tests indicate some fairly
- interesting results when compared to the MSC 5.1 procedures. Although
- I expected them to be slower than the MSC library procedures, they
- don't appear to be. Indeed, in some cases (e.g., realloc(s)), they
- seem to be faster. This was somewhat of a surprise. Additionally,
- they use about the same or less memory in most cases tested. The test
- program indicated above may be used to verify this (actually, you
- should do this to assure yourself that these procedures do what I have
- indicated).
-
- To do a comparison between these procedures and the MSC procedures, do
- the following:
-
- 1. Unarchive these procedures into a working directory.
-
- 2. Compile the procedures with the following command line:
-
- cl /c /AL *.c
-
- This will compile the program and procedures for testing the
- memory procedures contained in this archive.
-
- 3. Link the objects by executing the batch file 'linker.bat'.
-
- 4. Clear the screen and execute 'test'. The test program will
- perform a number of allocations, reallocations, and frees.
- At the end of each activity (e.g., a number of allocations),
- it will perform a heap dump to the file 'memory.new'.
-
- 5. Recompile the procedures with the following command line:
-
- cl /c /AL /DMSC *.c
-
- This will compile the program for testing with the MSC library
- procedures.
-
- 6. Line the objects by executing the batch file 'linker.bat'.
-
- 7. Clear the screen and execute 'test'. The same test will be
- performed as that indicated in 4 above with the exception that
- the heap dump will be written to a file called 'memory.msc'.
-
- 8. Compare the heap space used by both sets of procedures by
- comparing the memory dumps.
-
- You should try varying the parameters used in the test program to see
- what can occur for both sets of these procedures.
-
- Something you should be aware of is that these procedures require
- approximately 3000 more bytes of program space than the MSC procedures.
- However, as may be seen in the test above, if a large number of
- allocations and frees are performed, the total execution size may
- actually be smaller using these procedures. Additionally, you have the
- capability to release memory back to the system when necessary.
-
- If you have any comments, corrections, or improvements, please let me
- know. I am sure there are improvements which can be made.
-
-
- Willard Gersbacher - CompuServe User ID: (76117,2611)
-
-